package com.aat.restful.jersey.demo2.netty;

import org.glassfish.jersey.server.ApplicationHandler;
import org.glassfish.jersey.server.ResourceConfig;


import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

public class JerseyServletChannelInitializer extends ChannelInitializer<SocketChannel> {

	private JerseyServletHandler jerseyServletHandler;

	public JerseyServletChannelInitializer() {
		ResourceConfig rcf = new ResourceConfig();
		//rcf.register(JacksonFeature.class);//jackjson工具注册
		rcf.packages("com.aat.restful.jersey.demo2.web");
		this.jerseyServletHandler = new JerseyServletHandler(new ApplicationHandler(rcf));
	}

	protected void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		pipeline.addLast("decoder", new HttpRequestDecoder());
		pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
		pipeline.addLast("encoder", new HttpResponseEncoder());
		pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
		pipeline.addLast("handler", jerseyServletHandler);
	}

}
